package com.lalala;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Main {


    public static void main(String[] args) throws Exception{

        System.out.println("-----------START----------- GoodLuck :)");
        if (args.length < 5) {
            System.out.println("参数有误,指令示例: java -jar pachong.jar tianjin(城市名) localhost:3306(数据库连接地址及端口号) mydb(数据库名称) root(数据库账号) root(数据库密码)");
        }
        final String URL = "http://"+args[0]+".8684.cn";
        MysqlConnecter mc = new MysqlConnecter(args[1],args[2],args[3],args[4]);

        Connection connection = Jsoup.connect(URL)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; rv:47.0) Gecko/20100101 Firefox/47.0") //http请求的浏览器设置
                .timeout(5000)   //http连接时长
                .method(Connection.Method.GET);  //请求类型是get请求，http请求还是post,delete等方式
        Document doc = connection.get();
        Element body = doc.body();
        Elements elements = body.getElementsByClass("bus_kt_r1").get(0).select("a");
        for(Element lines : elements){
            //数字开头的线路各个页面绝对路径
            String linkNode = URL+lines.attr("href");
            System.out.println("市区线地址:"+linkNode);
            crawlerSecond(linkNode,mc,URL);
        }
        System.out.println("close mysql connection");
        mc.close();
        System.out.println("------------END------------ HappyEnding :)");
    }

    public static void crawlerSecond(String url,MysqlConnecter mc,String URL)throws Exception{
        Connection connection = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; rv:47.0) Gecko/20100101 Firefox/47.0") //http请求的浏览器设置
                .timeout(5000)   //http连接时长
                .method(Connection.Method.GET);  //请求类型是get请求，http请求还是post,delete等方式
        Document doc = connection.get();
        Element body = doc.body();
        Elements elements = body.getElementsByClass("stie_list").get(0).select("a");
        for(Element element : elements){
            //数字开头的线路各个页面绝对路径
            String linkNode = URL+element.attr("href");
            String name = element.text();
            System.out.println(name +"-url-"+linkNode);
            crawlerThrid(name,linkNode,mc);
        }
    }

    public static void crawlerThrid(String name, String url,MysqlConnecter mc)throws Exception {

        Connection connection = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; rv:47.0) Gecko/20100101 Firefox/47.0") //http请求的浏览器设置
                .timeout(5000)   //http连接时长
                .method(Connection.Method.GET);  //请求类型是get请求，http请求还是post,delete等方式
        Document doc = connection.get();
        Element body = doc.body();
        //取备注
        String remarks = body.getElementsByClass("bus_i_t4").get(0).text();
        Elements busLineTop = body.getElementsByClass("bus_line_top");
        //站点明细
        Elements elements = body.getElementsByClass("bus_site_layer");

        //上行路线,及总站数
        String up = busLineTop.get(0).select("strong").get(0).text();
        String upCountStr = busLineTop.get(0).getElementsByClass("bus_line_no").get(0).select("span").text();
        int upCount = Integer.valueOf(getNumbers(upCountStr));

        BusLine busLineUp = new BusLine();
        busLineUp.setBusName(name);
        busLineUp.setBusInfo(up);
        busLineUp.setBusNum(Integer.valueOf(getNumbers(name)));
        busLineUp.setStationNum(upCount);
        busLineUp.setRemarks(remarks);
        busLineUp.setRun((name.contains("尚未") || name.contains("停运"))?1:0);

        String lineSql = "INSERT INTO bus_line (bus_name,bus_info,station_num,remarks,bus_num,run) VALUES (\"" +
                busLineUp.getBusName() +"\",\""+busLineUp.getBusInfo()+"\",\""+busLineUp.getStationNum()+"\",\""+busLineUp.getRemarks()+"\",\""+busLineUp.getBusNum()+"\",\""+busLineUp.getRun()+"\") ";
        mc.update(lineSql);
        String selectIdSql = "SELECT * FROM bus_line WHERE bus_info = \""+ busLineUp.getBusInfo()+"\"";
        List<Map<String,String>> list = mc.select(selectIdSql,"bus_line");
        long id =Long.valueOf(String.valueOf(list.get(0).get("id")));
        busLineUp.setId(id);

        //上行线路站点处理
        System.out.println("加入线路:"+busLineUp.getBusInfo());
        int start = fixStationData(busLineUp,elements,0,mc);
        System.out.println(busLineUp.getBusName()+"----处理完成");

        if (busLineTop.size()>1){
            //下行路线,及总站数
            String down = busLineTop.get(1).select("strong").get(0).text();
            String downCountStr = busLineTop.get(1).getElementsByClass("bus_line_no").get(0).select("span").text();
            int downCount = Integer.valueOf(getNumbers(downCountStr));

            BusLine busLineDown = new BusLine();
            busLineDown.setBusName(name);
            busLineDown.setBusInfo(down);
            busLineDown.setBusNum(Integer.valueOf(getNumbers(name)));
            busLineDown.setStationNum(downCount);
            busLineDown.setRemarks(remarks);
            busLineDown.setRun((name.contains("尚未") || name.contains("停运"))?1:0);
            lineSql = "INSERT INTO bus_line (bus_name,bus_info,station_num,remarks,bus_num,run) VALUES (\"" +
                    busLineDown.getBusName() +"\",\""+busLineDown.getBusInfo()+"\",\""+busLineDown.getStationNum()+"\",\""+busLineDown.getRemarks()+"\",\""+busLineDown.getBusNum()+"\",\""+busLineDown.getRun()+"\") ";
            mc.update(lineSql);
            selectIdSql = "SELECT * FROM bus_line WHERE bus_info = \""+ busLineDown.getBusInfo()+"\"";
            List<Map<String,String>> list1 = mc.select(selectIdSql,"bus_line");
            id =Long.valueOf(String.valueOf(list1.get(0).get("id")));
            busLineDown.setId(id);

            //下行线路站点处理,可能没有下行线路
            System.out.println("加入线路:"+busLineDown.getBusInfo());
            fixStationData(busLineDown, elements, start,mc);
            System.out.println(busLineDown.getBusName()+"----处理完成");
        }
    }

    /**
     * @Author: ZhaoNing
     * @Description:处理站点信息
     * @param
     * @Date: 2017/6/29 17:44
     */
    public static int fixStationData(BusLine busLine, Elements elements,int start,MysqlConnecter mc) {
        int stationCount = 0;
        for (int i = start; i <elements.size() ; i++) {
            String stations = elements.get(i).select("a").text();
            String[] stationList = stations.split(" ");
            for (int j = 0; j < stationList.length ; j++) {
                BusStation busStation = new BusStation();
                busStation.setBusLineId(busLine.getId());
                busStation.setBusLineSort(stationCount+1);
                busStation.setStationName(stationList[j].replace("︵","（").replace("︶","）"));
                System.out.println(busStation.toString());
                String stationSql = "INSERT INTO bus_station (station_name,bus_line_id,bus_line_sort) VALUES (\""+
                        busStation.getStationName()+"\",\""+busStation.getBusLineId()+"\",\""+busStation.getBusLineSort()+ "\")";
                mc.update(stationSql);
                stationCount++;
                System.out.println(busLine.getBusName()+"加入站点:"+busStation.getStationName());
            }
            if (stationCount == busLine.getStationNum()){
                return i+1;
            }
        }
        return 0;
    }

    //截取数字
    public static String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
